package com.example.feifei.view.custom;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.feifei.newproject.R;

/**
 * Created by feifei on 2017/11/28.
 */

public class MyViewGroup extends ViewGroup {
    public MyViewGroup(Context context) {
        super(context);
        Button button1 = new Button(context);
        button1.setText("button1");

        Button button2 = new Button(context);
        button2.setText("button2");

        TextView textView = new TextView(context);
        textView.setText("textView");

        addView(button1);
        addView(button2);
        addView(textView);
        this.setBackgroundColor(getResources().getColor(R.color.bg_color));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        //设置该ViewGroup的大小
        int specSize_width = MeasureSpec.getSize(widthMeasureSpec);
        Log.e("tag","测量的宽度=="+specSize_width);
        int specSize_height = MeasureSpec.getSize(heightMeasureSpec);
        Log.e("tag","测量的高度=="+specSize_height);
        setMeasuredDimension(specSize_width, specSize_height);

        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
//                childView.measure(120, 120);
            childView.measure(MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(120,MeasureSpec.EXACTLY));
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int left = 0;
        int top = 10;
        for(int i = 0;i<childCount;i++){
            View childView = getChildAt(i);
            childView.layout(left,top,left+520,top+120);
//                LogUtils.e("得到子View宽高=="+childView.getWidth() +"  "+childView.getHeight());
            top += 130;
        }
    }
}
